<?php

require("./mpcamera/setup.php");

$smarty = new Smarty_MPCamera();

$action = filter_input(INPUT_GET, "action");
$submit = strtolower(filter_input(INPUT_POST, "submit"));

// OK
if (1 == 1) {
    // Select ChucVu
    $query = "select * from `chucvu`";
    $result = mysql_query($query);
    while ($row = mysql_fetch_array($result)) {
        $chucvu[] = $row;
    }
    $smarty->assign("chucvu", $chucvu);
    // End
}

// OK
if ($action == "add") {
    // Nothing
}

// OK
if ($submit == "add") {
    $success = 0;

    $ten = filter_input(INPUT_POST, "ten");
    $sdt = filter_input(INPUT_POST, "sdt");
    $chucvu = filter_input(INPUT_POST, "chucvu");

    $query = "
        SELECT 1
        FROM `lienhe_chucvu`, `lienhe`, `chucvu`
        WHERE `lienhe_chucvu`.`MaLH` = `lienhe`.`MaLH` AND
              `lienhe_chucvu`.`MaCV` = `chucvu`.`MaCV` AND
              `TenLH` = '$ten' AND
              `SDT` = '$sdt' AND
              `chucvu`.`MaCV` = $chucvu
        ;
    ";
    $result = mysql_query($query);
    if (mysql_num_rows($result) != 0) {
        $success = -1;
    } else {
        $query1 = "
            INSERT INTO `lienhe`(`TenLH`, `SDT`)
            VALUES('$ten','$sdt');
        ";
        $query2 = "
            INSERT INTO `lienhe_chucvu`(`MaCV`, `MaLH`)
            VALUES($chucvu,(SELECT `MaLH`
                            FROM `lienhe`
                            WHERE `TenLH`='$ten' AND `SDT`= '$sdt'));
        ";
        $result1 = mysql_query($query1);
        $result2 = mysql_query($query2);
        if ($result1 && $result2) {
            $success = 1;
        } else {
            $success = 0;
        }
    }
    $smarty->assign("success", $success);

    // Gọi hàm này thông qua ajax
    $ajax = filter_input(INPUT_POST, "ajax");

    if (isset($success) && isset($ajax)) {
        $query_maLHCV = "
            SELECT `MaLHCV`
            FROM `lienhe_chucvu`, `lienhe`, `chucvu`
            WHERE `lienhe_chucvu`.`MaCV` = `chucvu`.`MaCV`
            AND `lienhe_chucvu`.`MaLH` = `lienhe`.`MaLH`
            AND `chucvu`.`MaCV`=$chucvu
            AND `TenLH`=\"$ten\"
            AND `SDT` = \"$sdt\"            
            ;
        ";
        $result_maLHCV = mysql_query($query_maLHCV);
        $_maLHCV = mysql_fetch_array($result_maLHCV);
        $maLHCV = $_maLHCV['MaLHCV'];
        $data = array(
            "success" => $success,
            "maLHCV" => $maLHCV,
        );
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
        die();
    }
    // End
}

// OK
if ($action == "view") {
    $query = "
        SELECT `MaLHCV`, `TenLH`, `SDT`, `TenCV`
        FROM `lienhe_chucvu`, `lienhe`, `chucvu`
        WHERE `lienhe_chucvu`.`MaLH` = `lienhe`.`MaLH`
            AND `lienhe_chucvu`.`MaCV` = `chucvu`.`MaCV`
        ORDER BY `TenCV`, `TenLH`
        ;
    ";
    $result = mysql_query($query);
    while ($row = mysql_fetch_array($result)) {
        $lienhe[] = $row;
    }
    $smarty->assign("lienhe", $lienhe);
}

// OK
if ($action == "edit") {
    $found = 0;
    $_id = filter_input(INPUT_GET, "id");
    $id = $smarty->clean($_id, true);
    if ($id > 0) {
        $query = "
            SELECT `MaLHCV`, `TenLH`, `SDT`, `chucvu`.`MaCV` , `TenCV`
            FROM `lienhe_chucvu`, `lienhe`, `chucvu`
            WHERE `lienhe_chucvu`.`MaLH` = `lienhe`.`MaLH`
                AND `lienhe_chucvu`.`MaCV` = `chucvu`.`MaCV`
                AND `MaLHCV` = $id
            ORDER BY `TenCV`            
            ;
        ";
        $result = mysql_query($query);
        if (mysql_num_rows($result) == 1) {
            $found = 1;
            while ($row = mysql_fetch_array($result)) {
                $lienhe[] = $row;
            }
            $smarty->assign("lienhe", $lienhe);
        } else {
            header("Location: /lienhe");
        }
    } else {
        //$action = null;
        header("Location: /lienhe");
    }
}

// OK
if ($submit == "update") {
    $success = 0;

    $ten = filter_input(INPUT_POST, "ten");
    $sdt = filter_input(INPUT_POST, "sdt");
    $chucvu = filter_input(INPUT_POST, "chucvu");
    $maLHCV = filter_input(INPUT_POST, "maLHCV");

    $query = "
        SELECT 1
        FROM `lienhe_chucvu`, `lienhe`, `chucvu`
        WHERE `lienhe_chucvu`.`MaLH` = `lienhe`.`MaLH` AND
              `lienhe_chucvu`.`MaCV` = `chucvu`.`MaCV` AND
              `TenLH` = '$ten' AND
              `SDT` = '$sdt' AND
              `chucvu`.`MaCV` = $chucvu AND
              `MaLHCV` != $maLHCV
        ;
    ";
    $result = mysql_query($query);
    if (mysql_num_rows($result) != 0) {
        $success = -1;
    } else {
        $query1 = "
            UPDATE `lienhe`
            SET `TenLH` = '$ten', `SDT` = '$sdt'
            WHERE `MaLH` = (SELECT `MaLH`
                            FROM `lienhe_chucvu`
                            WHERE `MaLHCV` = $maLHCV)
            ;
        ";
        $query2 = "
            UPDATE `lienhe_chucvu`
            SET `MaCV` = $chucvu
            WHERE `MaLHCV` = $maLHCV
            ;
        ";
        $result1 = mysql_query($query1);
        $result2 = mysql_query($query2);
        if ($result1 && $result2) {
            $success = 1;
        } else {
            $success = 0;
        }
    }
    $smarty->assign("success", $success);
}

// OK
if ($action == "delete") {
    $success = 0;

    $maLHCV = filter_input(INPUT_GET, "id");

    $query = "
        SELECT *
        FROM `lienhe_chucvu`
        WHERE `MaLHCV`=$maLHCV
        ;
    ";
    $result = mysql_query($query);
    if (mysql_num_rows($result) == 0) {
        $success = -1;
    } else {
        $query = "
            DELETE FROM `lienhe_chucvu`
            WHERE `MaLHCV` = $maLHCV
            ;
        ";
        $query2 = "
            DELETE FROM `lienhe_chucvu`
            WHERE `MaLHCV` = $maLHCV
            ;
            DELETE FROM `lienhe`
            WHERE `MaLH` = (SELECT `MaLH`
                            FROM `lienhe_chucvu`
                            WHERE `MaLHCV`= $maLHCV)
            ;
        ";
        $result = mysql_query($query);
        if ($result) {
            $success = 1;
        } else {
            $success = 0;
        }
    }
    $smarty->assign("success", $success);
}
$smarty->assign("action", $action);

$smarty->display("lienhe.tpl");
